package idv.nightpig.lab02.service.impl;

import idv.nightpig.lab02.dao.UserDao;
import idv.nightpig.lab02.domain.User;
import idv.nightpig.lab02.service.Lab02Service;

import java.util.Collection;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class Lab02ServiceImpl implements Lab02Service {

    @Autowired
    private UserDao userDao;

    @Override
    public User doLogin(String account, String password) {
        User user = null;
        if (account != null && password != null) {
            User example = new User();
            example.setAccount(account);
            example.setPassword(password);
            List<User> users = userDao.findByExample(example);
            if (!users.isEmpty()) {
                user = users.get(0);
            }
        }
        return user;
    }

    @Override
    public Collection<User> findAllUser() {
        return userDao.findAll();
    }

    @Override
    public User findUserById(Long id) {
        return userDao.findById(id);
    }

    @Override
    public void persistUser(User user) {
        userDao.persist(user);
    }

    @Override
    public void removeUser(User user) {
        userDao.remove(user);
    }

}
